use std::collections::HashMap;

use core::fmt::{Display, Formatter};
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum CollectionType {
    #[default]
    #[serde(rename = "single")]
    Single,
    #[serde(rename = "pair")]
    Pair,
    #[serde(rename = "triple")]
    Triple,
    #[serde(rename = "tuple")]
    Tuple(i32),
    #[serde(rename = "array")]
    Array(i64),
}

impl Display for CollectionType {
    fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
        match self {
            CollectionType::Single => write!(f, "Single"),
            CollectionType::Pair => write!(f, "Pair"),
            CollectionType::Triple => write!(f, "Triple"),
            CollectionType::Tuple(size) => {
                let s = format!("Tuple({})", size).as_str();
                write!(f, "Tuple")
            }
            CollectionType::Array(size) => {
                let s = format!("Array({})", size).as_str();
                write!(f, "Array")
            }
        }
    }
}

impl From<CollectionType> for String {
    fn from(value_type: CollectionType) -> Self {
        match value_type {
            CollectionType::Single => "Single".into(),
            CollectionType::Pair => "Pair".into(),
            CollectionType::Triple => "Triple".into(),
            CollectionType::Tuple(size) => format!("Tuple({})", size),
            CollectionType::Array(size) => format!("Array({})", size),
        }
    }
}

impl From<String> for CollectionType {
    fn from(v_type: String) -> Self {
        match v_type {
            v_type if v_type == "Single" => CollectionType::Single,
            v_type if v_type == "Pair" => CollectionType::Pair,
            v_type if v_type == "Triple" => CollectionType::Triple,
            v_type if v_type.starts_with("Tuple(") => {
                //TODO 解析出size
                CollectionType::Tuple(10)
            }
            v_type if v_type.starts_with("Array(") => {
                //TODO 解析出size
                CollectionType::Array(10)
            }
            _ => CollectionType::Single,
        }
    }
}
